package com.ruoyi.client.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.dtflys.forest.annotation.Get;
import com.ruoyi.client.constants.RedisConstants;
import com.ruoyi.client.domain.vo.EchartsVo;
import com.ruoyi.client.domain.vo.OperVo;
import com.ruoyi.client.domain.vo.RecommendUserVo;
import com.ruoyi.client.service.impl.IndexService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("client/user/index")
public class BlogIndexController extends BaseController {
    @Autowired
    IndexService indexService;

    @Autowired
    StringRedisTemplate redisTemplate;
    @GetMapping("sign/{offset}/{result}")
    public R sign(@PathVariable Integer offset,@PathVariable Boolean result){
        String key = RedisConstants.SIGN + ":" + StpUtil.getLoginId();
        // 返回值为该位的旧值
        Boolean aBoolean = redisTemplate.opsForValue().setBit(key, offset, result);
        return R.ok();
    }
    /**
     * 关注推荐作者
     * @param id
     * @return
     */
    @PostMapping("attentionUser/{id}")
    public R attentionUser(@PathVariable Long id, @RequestParam("isAttention") Boolean isAttention){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        Boolean result = indexService.attentionUser(userId,id,isAttention);
        return R.ok(result);
    }
    /**
     * 获取推荐作者数据
     * @return
     */
    @GetMapping("recommendUser")
    public R<List<RecommendUserVo>> recommendUser(){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        List<RecommendUserVo> result = indexService.getRecommendUser(userId);
        return R.ok(result);
    }
    /**
     * 签到
     * @return
     */
    @GetMapping("sign/{offset}")
    public R sign(@PathVariable("offset") Integer offset){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        boolean sign = indexService.sign(userId,offset);
        Map<String, Object> map = indexService.signInfo(userId);
        return R.ok(map);
    }

    /**
     * 签到信息
     * @return
     */
    @GetMapping("signInfo")
    public R isSign(){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        Map<String,Object> info = indexService.signInfo(userId);
        return R.ok(info);
    }

    /**
     * 获取文章分类占比
     * @return
     */
    @GetMapping("getArticleProportion")
    public R<List<EchartsVo>> getArticleProportion(){
        List<EchartsVo> result = indexService.getArticleProportion();
        return R.ok(result);
    }
    @GetMapping("getAccessNum")
    public R<List<EchartsVo>> get(){
        List<EchartsVo> result = indexService.getAccessNum();
        return R.ok(result);
    }
    /**
     * 管理端
     */
    @GetMapping("getUserNum")
    public R<List<EchartsVo>> getUserNum(){
        List<EchartsVo> result = indexService.getUserNum();
        return R.ok(result);
    }
    /**
     * 获取评论数
     * @return
     */
    @GetMapping("getCommentNum")
    public R<List<EchartsVo>> getCommentNum(){
        List<EchartsVo> result = indexService.getCommentNum();
        return R.ok(result);
    }
    /**
     * 获取总文章量
     * @return
     */
    @GetMapping("getTotalArticleNum")
    public R<List<EchartsVo>> getTotalArticleNum(){
        List<EchartsVo> result = indexService.getTotalArticleNum();
        return R.ok(result);
    }
    /**
     * 获取总观看量
     * @return
     */
    @GetMapping("getTotalWatchNum")
    public R<List<EchartsVo>> getTotalWatchNum(){
        List<EchartsVo> result = indexService.getTotalWatchNum();
        return R.ok(result);
    }
    /**
     * 最新动态
     * @return
     */
    @GetMapping("getLastActivity")
    public R<List<OperVo>> getLastActivity(){
        List<OperVo> result = indexService.getLastActivity();
        return R.ok(result);
    }
}
